package sc.math;

/**
 * HermiteCurve is a CubicCurve that represents a cubic polynomial curve
 * defined by the end points and the tangent vectors at the end points.
 */
public class HermiteCurve implements CubicCurve {

    /**
     * Interpolates between the starting point p1 and end point p2 at i.
     * 
     * @param p1
     * 		Start point of the curve
     * @param t1
     * 		Tangent to how the curve leaves the start point
     * @param t2
     * 		Tangent to how the curve meets the end point
     * @param p2
     * 		End point of the curve
     * @param i
     * 		Interval along curve [0, 1]
     * 
     * @return the interval
     */
    public double interval(double p1, double t1, double t2, double p2, double i) {
    	double i2 = i * i;
    	double i3 = i2 * i;
    	double h1 = 2 * i3 - 3 * i2 + 1;
    	double h2 = -2 * i3 + 3 * i2;
    	double h3 = i3 - 2 * i2 + i;
    	double h4 = i3 - i2;
    	
    	return h1 * p1 + h2 * p2 + h3 * t1 + h4 * t2;
    }
    
    /**
     * Gets the index of the control point from where the curve starts.
     * 
     * This is a fairly pointless method, only needed to enable a generic
     * Spline2D. A BezierCurve starts at the first control point, so this
     * method returns 0.
     * 
     * @return index of the control point from where the curve starts
     */
    public int startIndex() {
        return 0;
    }
    
    /**
     * Gets the index of the control point from where the curve ends.
     * 
     * This is a fairly pointless method, only needed to enable a generic
     * Spline2D. A BezierCurve ends at the last control point, so this
     * method returns 3.
     * 
     * @return index of the control point from where the curve ends
     */
    public int endIndex() {
        return 3;
    }

}
